package com.railway.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.railway.bean.Train;
import com.railway.service.TrainService;
import com.railway.service.impl.TrainServiceImpl;
import com.railway.util.JSONUtil;

/**
 * Servlet implementation class TrainServlet
 */
@WebServlet("/TrainServlet")
public class TrainServlet extends HttpServlet {
	
	 protected void  doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   	request.setCharacterEncoding("Utf-8");
		   	Map<String,String[]> map = request.getParameterMap();
//		   	Set<String> keys = map.keySet(); //取出条件中所有的键
//			
//			for (String key : keys) {
//				System.out.println(key + map.get(key));
//				if(key.equals("date[]")) {
//					for (int i = 0; i < map.get(key).length; i++) {
//						System.out.println("----"+map.get(key)[i]);
//					}
//				}
//			}
//			String[] aa = request.getParameterValues("date[]");
//			System.out.println(aa);
		   	TrainService train = new TrainServiceImpl();
		   	List<Train> li = train.selectOneWayTrains(map);
		   	ObjectMapper om = new ObjectMapper();
		   	response.setCharacterEncoding("UTF-8"); //设置 后端给前端传回的编码格式为utf-8
		   	response.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式
		   	response.getWriter().print(om.writeValueAsString(li));
		   }

		   @Override
		   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		   	// TODO Auto-generated method stub
		   	 doPost(req, resp);
		   }

}
